package org.martyn.danbodroid.gui.activitys;

import java.util.ArrayList;

import org.martyn.danbodroid.DanboDroid;
import org.martyn.danbodroid.R;
import org.martyn.danbodroid.database.DatabaseHelper;
import org.martyn.danbodroid.database.ServersTable;
import org.martyn.danbodroid.gui.PreferenceRowDanbooru;
import org.martyn.danbodroid.gui.PreferenceRowSingle;
import org.martyn.danbodroid.gui.adapters.PreferencesAdapter;
import org.martyn.danbodroid.preferences.Preferences;
import org.martyn.danbooru.DanbooruConnectSettings;

import android.app.ListActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.ContextMenu;
import android.view.ContextMenu.ContextMenuInfo;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView.AdapterContextMenuInfo;
import android.widget.ListView;

public class MainPreferencesActivity extends ListActivity
{
	private static final int CONTEXT_MENU_REMOVE_ID = 1;

	private PreferencesAdapter preferencesAdapter;

	private DatabaseHelper databaseHelper;
	
	@Override
	protected void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);

		DanboDroid danboDroid = (DanboDroid) this.getApplication();

		databaseHelper = danboDroid.getDatabaseHelper();
		
		this.registerForContextMenu(getListView());

		buildAdapter();
	}

	private void buildAdapter()
	{
		// Build a list of server and search site settings objects to show
		ArrayList<DanbooruConnectSettings> dservers = ServersTable.getAll(databaseHelper);

		// Set the list items
		preferencesAdapter = new PreferencesAdapter(this, dservers);

		this.setListAdapter(preferencesAdapter);
	}

	@Override
	protected void onListItemClick(ListView l, View v, int position, long id)
	{
		Object item = this.getListAdapter().getItem(position);

		if (item instanceof PreferenceRowSingle)
		{
			PreferenceRowSingle button = (PreferenceRowSingle) item;

			if (button.getKey().equals(
					PreferencesAdapter.ADD_NEW_DANBOORUSERVER))
			{
				Intent intent = new Intent(this,
						DanbooruPreferencesActivity.class);

				this.startActivityForResult(intent, 0);
			}
			else if (button.getKey().equals(
					PreferencesAdapter.INTERFACE_SETTINGS))
			{
				this.startActivity(new Intent(this,
						InterfacePreferencesActivity.class));
			}
		}
		else if (item instanceof PreferenceRowDanbooru)
		{
			Intent intent = new Intent(this, DanbooruPreferencesActivity.class);

			intent.putExtra(
					DanbooruPreferencesActivity.DANBOORU_PREFERENCES_ACTIVITY_DATA_NAME,
					((PreferenceRowDanbooru) item).getDanbooruConnectSettings().id);

			this.startActivityForResult(intent, 0);
		}
	}

	@Override
	public void onCreateContextMenu(
			ContextMenu menu,
			View v,
			ContextMenuInfo menuInfo)
	{
		super.onCreateContextMenu(menu, v, menuInfo);

		Object item = preferencesAdapter
				.getItem((int) ((AdapterContextMenuInfo) menuInfo).id);

		if (item instanceof PreferenceRowDanbooru)
		{
			menu.add(0, CONTEXT_MENU_REMOVE_ID, 0,
					R.string.context_menu_remove_danboorurow);
		}
	}

	@Override
	public boolean onContextItemSelected(MenuItem item)
	{
		Object selected = preferencesAdapter
				.getItem((int) ((AdapterContextMenuInfo) item.getMenuInfo()).id);

		if (selected instanceof PreferenceRowDanbooru)
		{
			if (item.getItemId() == CONTEXT_MENU_REMOVE_ID)
			{
				PreferenceRowDanbooru preferenceRowDanbooru = (PreferenceRowDanbooru) selected;

				ServersTable.delete(databaseHelper, preferenceRowDanbooru.getDanbooruConnectSettings().id);

				Preferences.storeSelectedServerSettings(this, -1);
				
				buildAdapter();
				return true;
			}
		}

		return false;
	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data)
	{
		buildAdapter();
	}
}
